var loadLine,loadTrack,clearMap;
require(['gaodeMap'], function () {
	var map = new MapGaoDe(107.49023438, 36.24427318, 2),
	stmarker,
    endmarker,
    loadMask,
    stationMarkers = [],					//车站Marker集合
    polyLine,									//公交线路
    trackLine;									//轨迹线路
	
	$('#iCenter').css({
		width : $(window).width() -10,
		height : 450
	});
	
	/**
	 * 清除线路
	 */
    function clearPolyline() {
        if (polyLine) {
            polyLine.setMap(null);
            polyLine = null;
        }
    }
    
    /**
     * 清除地图上面的标记及线路
     */
    clearMap = function(){
        clearPolyline();
        if (stmarker) {
            stmarker.setMap(null);
        }
        if (endmarker) {
            endmarker.setMap(null);
        }
        if (stationMarkers.length) {
            for (var i = 0; i < stationMarkers.length; i++) {
                stationMarkers[i].setMap(null);
            }
            stationMarkers.splice(0, stationMarkers.length);
        }
        if (trackLine) {
            trackLine.setMap(null);
            trackLine = null;
        }
    };
    
	/**
	 * 查找线路地图信息
	 */
    loadLine = function(city, lineName, carId) {
    	map = new MapGaoDe(107.49023438, 36.24427318, 2);
        AMap.service(["AMap.LineSearch"], function () {
            var linesearch = new AMap.LineSearch({
                pageIndex: 1,
                city: city,
                pageSize: 1,
                extensions: 'all'
            });
            linesearch.search(lineName, function (status, result) {
                if (status === 'complete' && result.info === 'OK') {
                    var lineNum = result.lineInfo.length;
                    if (lineNum) {
                        var lineArr = result.lineInfo;
                        var pathArr = lineArr[0].path;
                        var stops = lineArr[0].via_stops;
                        var startPot = stops[0].location;
                        var endPot = stops[stops.length - 1].location;
                        var stationsLen = lineArr[0].via_stops.length;
                        for (var i = 0; i < stationsLen; i++) {
                            stationMarkers.push(new AMap.Marker({
                                map: map.map,
                                position: lineArr[0].via_stops[i].location,
                                offset: new AMap.Pixel(-3, -3),
                                icon: $.getProjectName() + "/content/images/way_btn2.png",
                                title: lineArr[0].via_stops[i].name,
                                zIndex: 13
                            }));
                        }
                        drawbusLine(startPot, endPot, pathArr);
                    }
                } else {
                    $.messager.alert('提示', '未找到对应的线路');
                }
            });
        });
    };
    
    /**
     * 绘制线路
     */
    function drawbusLine(startPot, endPot, pathArr) {
        //绘制起点，终点
        stmarker = new AMap.Marker({
            map: map.map,
            position: [startPot.lng, startPot.lat], //基点位置
            icon: $.getProjectName() + "/content/images/start_flag.png",
            zIndex: 13
        });
        endmarker = new AMap.Marker({
            map: map.map,
            position: [endPot.lng, endPot.lat], //基点位置
            icon: $.getProjectName() + "/content/images/end_flag.png",
            zIndex: 13
        });

        polyLine = new AMap.Polyline({
            map: map.map,
            path: pathArr,
            strokeColor: "#EA03B6",//线颜色
            strokeWeight: 5//线宽
        });
        map.map.setZoom(17);
        map.map.setFitView();
    }
    
  	/**
  	 * 根据车辆ID查找偏移数据并画出偏移路线
  	 */
    loadTrack = function(carId, beginTime, endTime) {
    	loadMask = $.createMask($("#iCenter"));
        $.ajaxExt({
            url: "/operationManage/loadShiftRoute.htm",
            data: {
                carId: carId,
                beginTime: beginTime,
                endTime: endTime
            }
        }).done(function (data) {
            if (!data) {
                $.messager.alert("提示", "没有轨迹数据", "info");
            } else {
                var poiontArr = [];
                var rcrdId = data[0].rcrdId;
                if(data.length == 1){
                	var marker = new AMap.Marker({
                        icon: "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
                        position: [data[0].loLgt, data[0].loLtt]
                    });
                    marker.setMap(map.map);
                }
                console.info(data);
                for (var i = 0; i < data.length; i++) { // 画线数组赋值
                	if(rcrdId != data[i].rcrdId){
                		trackLine = new AMap.Polyline({
                			map: map.map,
                			path: poiontArr,
                			strokeColor: "#3366FF",
                			strokeOpacity: 1,
                			strokeWeight: 5,
                			strokeStyle: "solid",
                			strokeDasharray: [10, 5]
                		});
                		rcrdId = data[i].rcrdId;
                		poiontArr = [];
                	}
                	if (data[i].loLgt != 0 || data[i].loLgt != 0) {
                		poiontArr.push([data[i].loLgt, data[i].loLtt]);
                	}
                }
                trackLine = new AMap.Polyline({
        			map: map.map,
        			path: poiontArr,
        			strokeColor: "#3366FF",
        			strokeOpacity: 1,
        			strokeWeight: 5,
        			strokeStyle: "solid",
        			strokeDasharray: [10, 5]
        		});
            }
        });
        loadMask.remove();
    }
});